package LeetcodeTest_twenty_seven;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class findMode
{
    public static void main(String[] args)
    {
        /*HashMap<Integer,Integer> map=new HashMap<Integer, Integer>();
        map.put(1,3);
        System.out.println(map.containsKey(2));*/

        TreeNode root=new TreeNode();
        root=Solution.createTree(root);
        int[] result=Solution.findMode(root);
    }
}
class Solution
{
    public static int maxCount=0;
    public static ArrayList<Integer> node=new ArrayList<>();
    public static int[] findMode(TreeNode root)
    {
        HashMap<Integer,Integer> map=new HashMap<Integer, Integer>();
        difs1(root,map);
        for(Integer key:map.keySet())
        {
            if(map.get(key)==maxCount)
                node.add(key);
        }
        int[] answer=new int[node.size()];
        for(int i=0;i<node.size();i++)
            answer[i]=node.get(i);
        maxCount=0;
        node.clear();
        return answer;
    }
    public static void difs1(TreeNode T, HashMap<Integer,Integer> map)
    {
        if(T!=null)
        {
            if(!map.containsKey(T.val))
            {
                map.put(T.val, 0);
            }
            int a=map.get(T.val);
            map.put(T.val,a+1);
            maxCount=Math.max(maxCount,map.get(T.val));
            difs1(T.left,map);
            difs1(T.right,map);
        }
    }
    public static TreeNode createTree(TreeNode T)
    {
        Scanner reader=new Scanner(System.in);
        int a=reader.nextInt();
        TreeNode p=null;
        if (a==-1)
            p=null;
        else
            p = new TreeNode(a);
        T=p;
        if(T==null)
            return T;
        else
        {
            T.left = createTree(T.left);
            T.right = createTree(T.right);
        }
        return T;
    }
}
class TreeNode
{
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right)
    {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}